//
//  test4camera2.swift
//  Colorful
//
//  Created by fox on 2021/8/11.
//  Copyright © 2021 fox. All rights reserved.
//

import Foundation
import Foundation

import UIKit

class EnterpriseRegistrationViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        addViews()

    }

    func addViews(){
        self.view .backgroundColor=UIColor.green

        let btn = UIButton(frame: CGRect(x: 30, y: 80, width: 50, height: 50))

        btn.setTitle("相册", for:UIControl.State.normal)

        btn.backgroundColor=UIColor.red

        btn.addTarget(self, action:"demoClicked", for: UIControl.Event.touchUpInside)

        self.view .addSubview(btn)

        

      

        let btn1 = UIButton(frame: CGRect(x: 90, y: 80, width: 50, height: 50))

        btn1.setTitle("相机", for:UIControl.State.normal)

        btn1.backgroundColor=UIColor.red

        btn1.addTarget(self, action:"demoCameera", for: UIControl.Event.touchUpInside)

        self.view .addSubview(btn1)

        

    }

    func demoCameera(){
        openCamera()

        

    }

    func demoClicked(){
        openAlbum()

    }

    //打开相册

    func openAlbum(){
        //判断设置是否支持图片库

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){

            //初始化图片控制器

            let picker = UIImagePickerController()

            //设置代理

            picker.delegate = self

            //指定图片控制器类型

            picker.sourceType = UIImagePickerController.SourceType.photoLibrary

            //设置是否允许编辑

            //            picker.allowsEditing = editSwitch.on

            //弹出控制器，显示界面

            self.present(picker, animated:true, completion: {
                () -> Void in

            })

        }else{
            print("读取相册错误")

        }

    }

    

    func openCamera(){
        if UIImagePickerController.isSourceTypeAvailable(.camera){

            

            //创建图片控制器

            let picker = UIImagePickerController()

            //设置代理

            picker.delegate = self

            //设置来源

            picker.sourceType = UIImagePickerController.SourceType.camera

            //允许编辑

            picker.allowsEditing = true

            //打开相机

            self.present(picker, animated:true, completion: { () -> Void in

                

            })

        }else{
            debugPrint("找不到相机")

        }

    }

    

    //选择图片成功后代理

    func imagePickerController(picker: UIImagePickerController,

        didFinishPickingMediaWithInfo info: [String :AnyObject]) {
            //查看info对象

            print(info)

            //获取选择的原图

        let image = info[UIImagePickerController.InfoKey.originalImage.rawValue]as! UIImage

            //            imageView.image = image

            //图片控制器退出

        picker.dismiss(animated: true, completion: {
                () -> Void in

            })

    }

}
